//
// Util.cs
//
// Copyright (C) 2008 Lukas Lipka <lukaslipka@gmail.com>
//

using System;
using System.IO;

using Gtk;
using Dashboard.Util;

namespace Dashboard.Mint {

	public class Util {

		public static Gdk.Pixbuf LoadStockIcon (string name, int size)
		{
			try {
				return Gtk.IconTheme.Default.LoadIcon (name, size, IconLookupFlags.NoSvg);
			} catch (Exception e) {
				//Console.WriteLine (e.ToString ());
			}
			
			return null;
		}

		public static Gdk.Pixbuf LoadMimeIcon (string mimetype, int size)
		{
			Gtk.IconTheme icon_theme = Gtk.IconTheme.Default;
			Gnome.IconLookupResultFlags result;

			string icon_name = Gnome.Icon.Lookup (icon_theme, null, null, null, null, mimetype, 
							      (Gnome.IconLookupFlags) 0, out result);
			
			if (icon_name == null)
				return null;
			
			Gtk.IconInfo icon_info = icon_theme.LookupIcon (icon_name, size, 0);
			
			if (icon_info == null)
				return null;

			try {
				return icon_info.LoadIcon ();
			} catch (Exception e) {
				Console.WriteLine (e.ToString ());
			}
			
			return null;
		}

		public static Gdk.Pixbuf LoadResourceIcon (string icon, int size)
		{
			string icons_dir = System.IO.Path.Combine (PathFinder.Installation, "icons");

			try {
				Gdk.Pixbuf pixbuf = new Gdk.Pixbuf (Path.Combine (icons_dir, icon));
				return pixbuf.ScaleSimple (size, size, Gdk.InterpType.Bilinear);
			} catch (Exception e) {
				Console.WriteLine (e.ToString ());
			}

			return null;
		}
	}
}
